

import HeadPrefab from "../../../component/HeadPrefab";
import { Logger } from "../../../engine/Logger";
import ViewBase from "../../../engine/ViewBase";
import { JhaoProto } from "../../../lib/net/protobuf/jhaoproto";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { SocketClient } from "../../../net/SocketClient";
import { ClubConfig } from "../../config/ClubConfig";
import { GameConfig } from "../../config/GameConfig";
import { RoomConfig } from "../../config/RoomConfig";
import { RoomConfig_huanghua_mj } from "../../config/RoomConfig_huanghua_mj";
import RoomConfig_Mj from "../../config/RoomConfig_Mj";
import { RoomModel } from "../../model/RoomModel";
import { RoomPlayer } from "../../model/RoomPlayer";
import { RoomTemplateModel } from "../../model/RoomTemplateModel";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubRoomDetail extends ViewBase {

    private roomObj: JhaoProto.IRoomTemplate
    private btnDeleteTemplate: cc.Node;
    private btnEnterRoom: cc.Node
    private btnReleaseRoom: cc.Node;
    private btnQuickJoin: cc.Node;
    private playerItem: cc.Node


    public onInitChild(roomObj: JhaoProto.IRoomTemplate) {
        this.roomObj = roomObj;
    }


    onEnable(): void {
        super.onEnable();
        this.playerItem = this.findChild(this.node, "playerContainer/playerItem")
        this.hideAllChildren(this.playerItem.parent)
        let controlNode: cc.Node = this.findChild(this.node, "controlNode")
        this.btnReleaseRoom = this.findChild(controlNode, "btnReleaseRoom")
        this.btnReleaseRoom.active = false
        UIHelper.onAddEvent(this.btnReleaseRoom, () => {
            if (this.roomObj instanceof RoomModel) {
                // ClubSocketClient.req_club_release_room(this.roomObj.clubid, this.roomObj.room_id, () => {
                //     this.closeView()
                // })
            }
        })
        this.btnQuickJoin = this.findChild(controlNode, "btnQuickJoin")
        this.btnQuickJoin.active = true
        UIHelper.onAddEvent(this.btnQuickJoin, () => {
            // this.roomObj["conf"]["name"] = this.roomObj["name"]
            Logger.log("btnQuickJoin==", this.roomObj)
            SocketClient.req_create_room(this.roomObj)
        })
        this.btnEnterRoom = this.findChild(controlNode, "btnEnterRoom")
        this.btnEnterRoom.active = false;
        this.btnDeleteTemplate = this.findChild(controlNode, "btnDeleteTemplate")
        if (UserModel.instance.nowClub.actor == ClubConfig.Club_Actor_Type.general) {
            this.btnDeleteTemplate.active = false
        }
        else {
            let hadQuanxian: boolean = false
            if (UserModel.instance.nowClub.actor == ClubConfig.Club_Actor_Type.owner) {
                hadQuanxian = true
            }
            else if (UserModel.instance.nowClub.actor == ClubConfig.Club_Actor_Type.administrator) {
                hadQuanxian = true
            }
            this.btnDeleteTemplate.active = hadQuanxian;
            UIHelper.onAddEvent(this.btnDeleteTemplate, () => {
                ClubSocketClient.req_club_delete_room_template(this.roomObj.id, this.roomObj.clubid, () => {
                    this.closeView()
                })
            })
        }
        this.refresh()
    }

    private refreshPlayerList() {
        if (this.roomObj instanceof RoomModel) {
            for (let i = 0; i < this.roomObj.playerList.length; i++) {
                let player: RoomPlayer = this.roomObj.playerList[i]
                let playerItem: cc.Node = this.playerItem.parent.children[i]
                if (!playerItem) {
                    playerItem = cc.instantiate(this.playerItem)
                    playerItem.parent = this.playerItem.parent
                }
                playerItem.active = true
                let head: HeadPrefab = this.getChild(playerItem, "head", HeadPrefab)
                head.setHead(player.headimg, player.lv)
                let txtId: cc.Label = this.getChild(playerItem, "txtId", cc.Label)
                txtId.string = "ID:" + player.userid
                let txtInvite: cc.Label = this.getChild(playerItem, "txtInvite", cc.Label)
                if (player.invite) {
                    txtInvite.string = "上级:" + player.invite
                } else {
                    txtInvite.string = "上级:"
                }
                let txtScore: cc.Label = this.getChild(playerItem, "txtScore", cc.Label)
                txtScore.string = "分数:" + player.score
            }
        }
    }

    private refresh() {
        Logger.log("ViewClubRoomDetail refresh", this.roomObj)
        this.hideAllChildren(this.playerItem.parent)
        if (this.roomObj instanceof RoomModel) {
            this.btnDeleteTemplate.active = false
            this.btnQuickJoin.active = false

            if (UserModel.instance.nowClub.actor == ClubConfig.Club_Actor_Type.administrator ||
                UserModel.instance.nowClub.actor == ClubConfig.Club_Actor_Type.owner) {
                this.btnReleaseRoom.active = true
            }
            this.refreshPlayerList()
        }
        let txtGameInfo: cc.Label = this.getChild(this.node, "txtGameInfo", cc.Label)

        txtGameInfo.string = this.roomObj.roomConfigBase.jushu + "局," + this.roomObj.roomConfigBase.renshu + "人"
        txtGameInfo.string += "" + GameConfig.GameNameList[this.roomObj.gameType]
        txtGameInfo.string += "," + RoomConfig.cost_type_name[this.roomObj.roomConfigBase.costType]
        if (this.roomObj.roomConfigBase.costType == RoomConfig.cost_type.pingjun) {
            txtGameInfo.string += "," + this.roomObj.roomConfigBase.cost / this.roomObj.roomConfigBase.renshu + RoomConfig.costItemName[this.roomObj.roomType]
        } else {
            txtGameInfo.string += "," + this.roomObj.roomConfigBase.cost + RoomConfig.costItemName[this.roomObj.roomType]
        }
        txtGameInfo.string += ",底分:" + this.roomObj.roomConfigBase.difen
        txtGameInfo.string += ",距离限制:" + this.roomObj.roomConfigBase.distanceLimit
        if (this.roomObj.roomConfigBase.tuoguanTime == 0) {
            txtGameInfo.string += ",不允许托管"
        }
        else {
            txtGameInfo.string += ",允许托管(" + this.roomObj.roomConfigBase.tuoguanTime + "秒)"
        }
        txtGameInfo.string += ",解散时间(" + this.roomObj.roomConfigBase.dismissTime + "秒)"
        if (this.roomObj.roomConfigBase.limitEmoji) {
            txtGameInfo.string += ",禁用表情"
        }
        if (this.roomObj.roomConfigBase.limitVoice) {
            txtGameInfo.string += ",禁用语音"
        }
        if (this.roomObj.roomConfigBase.limitDropItem) {
            txtGameInfo.string += ",禁止扔道具"
        }
        if (this.roomObj.roomType == GameConfig.Game_Type.Mj) {
            txtGameInfo.string += "," + RoomConfig_Mj.dianpao_type_name[this.roomObj.roomConfigMj.dianpaoType]
        }

    }

}
